package com.powernode.controller;

/**
 * @version 1.0
 * @Date 2024/8/19 17:15
 * @Author 付~~~
 */

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.powernode.domain.OrdersList;
import com.powernode.model.Result;
import com.powernode.service.OrdersListService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 订单业务模块的控制层
 */
@Api(tags = "订单业务的接口管理")
@RestController
@RequestMapping("order/order")
public class SysOrdersListController {

    @Autowired
    private OrdersListService orderService;

    /**
     * 多条件分页查询订单
     * @param current 当前页码
     * @param size 每页显示数
     * @param orderNumber 订单编号
     * @param startTime 订单开始时间
     * @param endTime 结束时间
     * @param status 订单状态
     * @return
     */
    @ApiOperation("多条件分页查询订单")
    @GetMapping("page")
    @PreAuthorize("hasAuthority('order:order:page')")
    public Result<Page<OrdersList>> loadOrderPage(@RequestParam Long current,
                                                  @RequestParam Long size,
                                                  @RequestParam(required = false) String orderNumber,
                                                  @RequestParam(required = false) Integer status,
                                                  @RequestParam(required = false) Date startTime,
                                                  @RequestParam(required = false) Date endTime){

        // 创建一个订单分页对象
        Page<OrdersList> page = new Page<>(current, size);

        // 多条件分页查询订单
        page = orderService.queryOrderPage(page,orderNumber,status,startTime,endTime);

        return Result.success(page);
    }

    /**
     * 根据订单编号查询详细订单信息
     * @param orderNumber 订单编号
     * @return
     */
    @ApiOperation("根据订单编号查询详细订单信息")
    @GetMapping("orderInfo/{orderNumber}")
    @PreAuthorize("hasAuthority('order:order:info')")
    public Result<OrdersList> loadOrderListsDetail(@PathVariable Long orderNumber){
        OrdersList ordersList = orderService.queryOrderListsDetailByOrderNumber(orderNumber);
        return Result.success(ordersList);
    }

    /**
     * 导出销售记录
     * @return
     */
    @ApiOperation("导出销售记录")
    @GetMapping("soldExcel")
    @PreAuthorize("hasAuthority('order:order:soldExcel')")
    public Result<String> exportSoleOrderRecordExcel(){
        // 查询所有销售记录
        List<OrdersList> list = orderService.list(new LambdaQueryWrapper<OrdersList>()
                .orderByDesc(OrdersList::getCreateTime)
        );
        String fileName = "simpleWrite" + System.currentTimeMillis() + ".xlsx";
        // 这里 需要指定写用哪个class去写，然后写到第一个sheet，名字为模板 然后文件流会自动关闭
        // 如果这里想使用03 则 传入excelType参数即可
        EasyExcel.write(fileName, OrdersList.class).sheet("模板111").doWrite(list);
        return Result.success(null);
    }


}
